import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)


const routes = [
    {
        path: '/',
        name: 'home',
        component: resolve => require(['../views/Home.vue'], resolve),
        meta: { title: '首页',istrun: true}
    },
   /* {
        path: '/category/:cate',
        name: 'category',
        component: () => import('../views/Home.vue'),
        meta: { title: '分类', params: 'cate',istrun: true}
    }, */
    {
        path: '/search/:words',
        name: 'search',
        component: () => import('../views/Home.vue'),
        meta: { title: '搜索', params: 'words',istrun: true}
    },
	{
	    path: '/message',
	    name: 'message',
	    component: resolve => require(['../views/message.vue'], resolve), 
	    meta: { title: '留言',istrun: true}
	},
    {
        path: '/about',
        name: 'about',
        component: resolve => require(['../views/About.vue'], resolve), 
        meta: { title: '关于',istrun: true}
    },
    {
        path: '/friend',
        name: 'friend',
        component: resolve => require(['../views/Friend.vue'], resolve),
        meta: { title: '友链',istrun: true}
    },
    {
        path: '/article/:id',
        name: 'article',
        component: resolve => require(['../views/Article.vue'], resolve),
        meta: { title: '文章',istrun: true}
    },
	{
	    path: '/articles/:id',
	    name: 'articles',
	    component: resolve => require(['../views/Articles.vue'], resolve),
	    meta: { title: '文章列表',istrun: true}
	},
	{
	    path: '/login',
	    name: 'login',
	    component: resolve => require(['../views/login.vue'], resolve),
	    meta: { title: '登录',istrun: false}
	},
	{
	    path: '/qq_redirect',
	    name: 'qq_redirect',
	    component: resolve => require(['../views/qq_redirect.vue'], resolve),
	    meta: { title: '登录成功提示',istrun: false}
	},
	{
	    path: '/userInfo',
	    name: 'userInfo',
	    component: () => import('../views/UserInfo.vue'),
	    meta: { title: '个人中心',istrun: true}
	},
	{
	    path: '/diarys',
	    name: 'diarys',
	    component: () => import('../views/diarys.vue'),
	    meta: { title: '日记',istrun: true}
	},
	{
	    path: '/diary/:id',
	    name: 'diary',
	    component: () => import('../views/diary.vue'),
	    meta: { title: '日记内容',istrun: true}
	}
]
for(var k in routes){
	routes[k].meta.title= process.env.VUE_APP_TITLE + " | "+routes[k].meta.title;
}
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    let title = ''
    if (to.meta.params){
        //title = `${to.meta.title}:${to.params[to.meta.params] || ''} - ${title}`
		title = `${to.meta.title}:${to.params[to.meta.params] || ''}`
    }else {
        //title = `${to.meta.title} - ${title}`
		title = `${to.meta.title}`
    }
    document.title = title
    if (to.path !== from.path) {
        store.dispatch('setLoading', true);
    }
    next();
})
router.afterEach((to, from) => {
    // 最多延迟 关闭 loading
    setTimeout(() => {
        store.dispatch('setLoading', false);
    }, 500)
})
export default router
